<template>
  <HomePanel title="人气推荐" sub-title="人气爆款 不容错过" ref="target">
    <template #main>
      <ul class="goods-list">
        <li v-for="item in list" :key="item.id">
        <RouterLink to="/">
          <img v-img-lazy="item.picture" alt="">
          <p class="name">{{item.title}}</p>
          <p class="desc">{{item.alt}}</p>
        </RouterLink>
        </li>
      </ul>
    </template>
  </HomePanel>
</template>

<script>
// 引入组件
import HomePanel from './HomePanel.vue'
import { ref } from 'vue'
// 引入接口
import { findHot } from '@/api/home'
// 引入监听组件懒加载的方法
import { useObserve } from '@/useFn'
export default ({
  components: {
    HomePanel
  },
  setup () {
    //   声明一个空数组
    const listRef = ref([])
    // 定义方法发起请求
    async function loadList () {
      const res = await findHot()
      //   给空数组赋值
      listRef.value = res.data.result
    }
    // 在onMounted中发起请求
    // onMounted(() => {
    //   loadList()
    // })
    // 传一个调用接口的函数进去   返回值是一个taget  通过ref绑定到dom元素上
    const { target } = useObserve(loadList)

    return {
      target,
      list: listRef
    }
  }
})
</script>

<style scoped lang='less'>
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
    .price {
      color: @priceColor;
    }
  }
}
</style>
